package com.my.shetuan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.shetuan.controller.utils.SYSTENET;
import com.my.shetuan.entity.Admin;
import com.my.shetuan.mapper.AdminMapper;
import com.my.shetuan.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private AdminMapper adminMapper;

    @GetMapping(value = "/")
    public String loginMenu(Model model){
        return "login";
    }

    @PostMapping("/login")
    public String login(Admin admin, HttpSession session, Model model){
        //TODO mapper方法,返回admin对象
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        Admin admin1 = adminMapper.selectOne(wrapper.eq("username", admin.getUsername())
                                                .eq("password", admin.getPassword()));
        if (!ObjectUtils.isEmpty(admin1)){
            session.setAttribute("loginUser",admin1);
            return "redirect:/main.html";
        }else {
            model.addAttribute(SYSTENET.MSG, "用户名或密码不正确，请重新登录");
            return "login";
        }

//        if ("admin".equals(admin.getUsername()) && "123456".equals(admin.getPassword())){
//            //将得到的数据存到session,用于后端显示
////            session.setAttribute("loginUser",admin);
//            model.addAttribute(String.valueOf(SYSTENET.CODE),200);
//            //index.html是一个刷新的路径，不是真正的跳到index页面去
//            return "redirect:/main.html";
//        }else {
//            model.addAttribute(SYSTENET.MSG, "用户名或密码不正确，请重新登录");
//            return "login";
//        }
    }

    //真正的跳转路径
    @GetMapping("/main.html")
    public String mainPage(){
        return "index";
    }

}
